package com.kakaxi.mybilibili.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * 网络连接检测工具
 * Created by kakaxi on 16/8/13.
 */

public class NetworkCheckUtil {

    /** 连接管理器 **/
    private static ConnectivityManager sConnectivityManager;

    /**
     * 判断是否链接网络
     *
     * @param context 上下文
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo != null) {
            return activeNetworkInfo.isConnected();
        }
        return false;
    }

    /**
     * 判断是否连接无线网络
     *
     * @param context 上下文
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo != null && activeNetworkInfo.getType() == ConnectivityManager
                .TYPE_WIFI) {
            return activeNetworkInfo.isConnected();
        }
        return false;
    }

    /**
     * 判断是否连接流量网络
     *
     * @param context 上下文
     * @return
     */
    public static boolean isMobileConnected(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo != null && activeNetworkInfo.getType() == ConnectivityManager
                .TYPE_MOBILE) {
            return activeNetworkInfo.isConnected();
        }
        return false;
    }


    /**
     * 获取网络连接的类型数字
     *
     * @param context 上下文
     * @return
     */
    public static int getConnectedType(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            return activeNetworkInfo.getType();
        }
        return -1;
    }

    /**
     * 获取网络连接类型名
     *
     * @param context 上下文
     * @return
     */
    public static String getConnectedName(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            return activeNetworkInfo.getTypeName();
        }
        return null;
    }

    /**
     * 判断流量网络的连接类型名2/3/4g
     *
     * @param context 上下文
     * @return
     */
    public static String getMobileConnectedName(Context context) {
        if (isMobileConnected(context)) {
            NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);

            String mobileTypeName; // 流量网络类型名
            String subTypeName = getActiveNetworkInfo(context).getSubtypeName(); // 从系统中获取的代号类型名
            int subType = activeNetworkInfo.getSubtype(); // 从系统中获取的代号类型数字
            // 通过类型数字判断所连接的网络类型
            switch (subType) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
                    mobileTypeName = "2G";
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
                    mobileTypeName = "3G";
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                    mobileTypeName = "4G";
                    break;
                default:
                    if (subTypeName.equalsIgnoreCase("TD-SCDMA") || subTypeName.equalsIgnoreCase
                            ("WCDMA") || subTypeName.equalsIgnoreCase("CDMA2000")) {
                        mobileTypeName = "3G";
                    } else {
                        mobileTypeName = subTypeName;
                    }
                    break;
            }
            return mobileTypeName;
        }
        return null;
    }

    /**
     * 返回正在活动的连接信息
     *
     * @param context 上下文
     * @return
     */
    public static NetworkInfo getActiveNetworkInfo(Context context) {
        initConnManager(context);
        if (sConnectivityManager != null) {
            return sConnectivityManager.getActiveNetworkInfo();
        }
        return null;
    }

    /**
     * 初始化网络连接管理器
     *
     * @param context 上下文
     */
    private static void initConnManager(Context context) {
        if (context != null && sConnectivityManager == null) {
            sConnectivityManager = (ConnectivityManager) context.getSystemService
                    (Context.CONNECTIVITY_SERVICE);
        }
    }


}
